package com.zhang.service.Impl;

import com.zhang.domain.User;
import com.zhang.mapper.LogininfoMapper;
import com.zhang.mapper.UserMapper;
import com.zhang.query.UserQuery;
import com.zhang.service.IUserService;
import com.zhang.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author zh
 * @create 2022-03-02 18:56
 */
@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private UserMapper mapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private LogininfoMapper logininfoMapper;
    @Resource
    private JavaMailSender javaMailSender;
    @Override
    public AjaxResult getCode(String phone) {
        User user = mapper.getCode(phone);
        if(user!=null){
            return AjaxResult.me(false,"该手机号已经被注册");
        }else {
            String value = (String) redisTemplate.opsForValue().get(phone);
            String code = CodeUtils.getCode();
            if(value==null){
                SMSUtils.sendSMS(phone, code);
                redisTemplate.opsForValue().set(phone,code);
                redisTemplate.expireAt(phone,new Date(System.currentTimeMillis()+3*60*1000));
                return AjaxResult.me();
            }else {
                if(redisTemplate.getExpire(phone)<=0){
                    return AjaxResult.me(false,"验证码已经过期");
                }else {
                    SMSUtils.sendSMS(phone, code);
                    return AjaxResult.me();
                }
            }
        }
    }

    @Override
    public AjaxResult regist(UserQuery query) {
        if(!StringUtils.isEmpty(query.getPhone())&&!StringUtils.isEmpty(query.getCode())&&!StringUtils.isEmpty(query.getPwd())&&!StringUtils.isEmpty(query.getRepwd())){
            if(query.getPwd().equals(query.getRepwd())){
                String value = (String) redisTemplate.opsForValue().get(query.getPhone());
                if(redisTemplate.getExpire(query.getPhone())>0){
                    if(value.equals(query.getCode())){
                        query.setDisable(1);
                        String salt = StrUtils.getComplexRandomString(32); //盐，，，随机串，安全
                        //2 通过这个盐值加密
                        String md5Pwd = MD5Utils.encrypByMd5(query.getPwd() + "zhang" + salt + "hou");
                        query.setPwd(md5Pwd);
                        query.setSalt(salt);
                        logininfoMapper.addUserMsg(query);
                        query.setState(1);
                        mapper.addUserMsg(query);
                        return AjaxResult.me();
                    }else {
                        return AjaxResult.me(false,"验证码不正确");
                    }
                }else {
                    return AjaxResult.me(false,"验证码已经过期");
                }
            }else {
                return AjaxResult.me(false,"二次用密码不一致");
            }
        }else {
            return AjaxResult.me(false,"用户名密码或验证码不能为空");
        }
    }

    @Override
    public AjaxResult getemailCode(String email) {
        User user = mapper.getemalicode(email);
        if(user!=null){
            return AjaxResult.me(false,"该邮箱已经被注册");
        }else {
            String value = (String) redisTemplate.opsForValue().get(email);
            String code = CodeUtils.getCode();
            if(value==null){
                sendMail(code,email);
                redisTemplate.opsForValue().set(email,code);
                redisTemplate.expireAt(email,new Date(System.currentTimeMillis()+30*60*1000));
                return AjaxResult.me();
            }else {
                if(redisTemplate.getExpire(email)<=0){
                    return AjaxResult.me(false,"验证码已经过期");
                }else {
                    sendMail(code,email);
                    return AjaxResult.me();
                }
            }
        }
    }

    @Override
    public AjaxResult emailregist(UserQuery query) {
        if(!StringUtils.isEmpty(query.getEmail())&&!StringUtils.isEmpty(query.getCode())&&!StringUtils.isEmpty(query.getPwd())&&!StringUtils.isEmpty(query.getRepwd())){
            if(query.getPwd().equals(query.getRepwd())){
                String value = (String) redisTemplate.opsForValue().get(query.getEmail());
                if(redisTemplate.getExpire(query.getEmail())>0){
                    if(value.equals(query.getCode())){
                        query.setDisable(1);
                        String salt = StrUtils.getComplexRandomString(32); //盐，，，随机串，安全
                        //2 通过这个盐值加密
                        String md5Pwd = MD5Utils.encrypByMd5(query.getPwd() + "zhang" + salt + "hou");
                        query.setPwd(md5Pwd);
                        query.setSalt(salt);
                        logininfoMapper.addUserMsg(query);
                        query.setState(1);
                        mapper.addUserMsg(query);
                        return AjaxResult.me();
                    }else {
                        return AjaxResult.me(false,"验证码不正确");
                    }
                }else {
                    return AjaxResult.me(false,"验证码已经过期");
                }
            }else {
                return AjaxResult.me(false,"二次用密码不一致");
            }
        }else {
            return AjaxResult.me(false,"用户名密码或验证码不能为空");
        }
    }

    public void sendMail(String code,String setTo){
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            //设置发送人
            mailMessage.setFrom("1209267145@qq.com");
            //邮件主题
            mailMessage.setSubject("激活码");
            //邮件内容
            mailMessage.setText(code);
            //收件人
            mailMessage.setTo(setTo);
            javaMailSender.send(mailMessage);
        }
}
